package com.betterwood.dragon.controllers;

import cn.dev33.satoken.stp.StpUtil;

import com.betterwood.dragon.core.common.JsonResult;
import com.betterwood.dragon.entity.dto.IdDTO;
import com.betterwood.dragon.entity.dto.LoginDTO;
import com.betterwood.dragon.entity.SysUser;
import com.betterwood.dragon.entity.dto.SysUserLoginDTO;
import com.betterwood.dragon.entity.dto.SysUserUpdateDTO;
import com.betterwood.dragon.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping(value = "/user")
@CrossOrigin
@Slf4j
@Api(tags = "系统用户模块")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "系统用户登陆")
    @RequestMapping(value = "/weblogin", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public JsonResult<Object> doLogin(@Valid @RequestBody SysUserLoginDTO sysUserLoginDTO) {
        String password = DigestUtils.md5DigestAsHex(sysUserLoginDTO.getPassword().getBytes());
        sysUserLoginDTO.setPassword(password);
        SysUser sysUser = sysUserService.getSysUserByNameAndPwd(sysUserLoginDTO.getUserName(),
                sysUserLoginDTO.getPassword());
        log.info("here is login B");
        if (sysUser == null) {
            return JsonResult.fail("账号或者密码错误");
        }
        log.info("here is login A");
        StpUtil.setLoginId(sysUser.getId());
        LoginDTO login = new LoginDTO();
        login.setToken(StpUtil.getTokenInfo().getTokenValue());
        login.setShopId(sysUser.getShopId());
        login.setRole(sysUser.getRole());
        login.setUserId(sysUser.getId());

        login.setUserName(sysUser.getUserName());

        return JsonResult.success(login);
    }

    @GetMapping("/list")
    public JsonResult<List<SysUser>> list() {
        // log.info("####" + StpUtil.getLoginIdAsString());
        return JsonResult.success(sysUserService.findAll());
    }

    @ApiOperation(value = "系统用户注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public JsonResult<Object> doRegister(@Valid @RequestBody SysUser record) {
        if (sysUserService.isExist(record.getUserName())) {
            return JsonResult.fail("用户名已经存在");
        }
        String password = DigestUtils.md5DigestAsHex(record.getPassword().getBytes());
        record.setPassword(password);
        record.setCreateTime(LocalDateTime.now());
        sysUserService.insert(record);
        return JsonResult.success(record);
    }

    @ApiOperation(value = "系统用户修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public JsonResult<Object> doUpdate(@RequestBody SysUser record)
            throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        if (record.getPassword() != null) {
            String password = DigestUtils.md5DigestAsHex(record.getPassword().getBytes());
            record.setPassword(password);
        }
        record.setUpdateTime(LocalDateTime.now());
        sysUserService.update(record);
        // Optional<SysUser> sysUser=sysUserService.queryById(record.getId());
        return JsonResult.success(record);
    }

    @ApiOperation(value = "系统用户删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public JsonResult<Object> doDelete(@Valid @RequestBody IdDTO id) {
        sysUserService.deleteById(id.getId());
        return JsonResult.success("系统用户删除成功");
    }

    @ApiOperation(value = "系统用户密码修改")
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public JsonResult<Object> updatePassword(@Valid @RequestBody SysUserUpdateDTO record)
            throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        String password = DigestUtils.md5DigestAsHex(record.getPassword().getBytes());
        Optional<SysUser> s = sysUserService.findOneById(record.getId());

        s.get().setPassword(password);
        s.get().setUpdateTime(LocalDateTime.now());
        sysUserService.update(s.get());
        return JsonResult.success("用户密码修改完成");
    }

}
